import socket,select
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 7776))
s.listen(10)

s.setblocking(False)
wlist = []
rlist = []
errlist = []

rlist.append(s)
dict_data=dict()
while True:
    read_list, write_list, e_list = select.select(rlist,wlist,errlist,2)
    for i in read_list:
        if i is s:
            try:
                conn, addr = i.accept()
                conn.setblocking(False)
                rlist.append(conn)
            except Exception:
                pass

        else:
            try:
                data = i.recv(1024)
                dict_data[i]=data
                print('收到信息: ', data.decode('utf8'))
                if not data:
                    i.close()
                    rlist.remove(i)
                    continue
                wlist.append(i)
                rlist.remove(i)
            except Exception:
                i.close()
                rlist.remove(i)
    for k in write_list:
        try:
            tell = input('---->')
            k.send(tell.encode('utf8'))
            rlist.append(k)
        except Exception:
            k.close()
        finally:
            wlist.remove(k)





